﻿'test check in
Public Class Frm_Liaison_Ref
    Dim refer As Reference
    Public Sub initialCombo()
        Comb_nom.SelectedIndex = -1
        Comb_adv.SelectedIndex = -1
    End Sub

    Private Sub Frm_Ref_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Partager.remplirClient(Comb_nom)
        Partager.remplirEnemie(Comb_adv)
    End Sub
    Public Sub remplirListViewClt()
        Partager.refObj.remplir_lstRefClt()

        List_Clients.Items.Clear()
        For Each khib As Partager.clt In Partager.refObj.lstRefClt
            List_Clients.Items.Add(New ListViewItem(New String() {khib.id, khib.nom}))
        Next
    End Sub

    Public Sub remplirListViewAdv()
        Partager.refObj.remplir_lstRefAdv()

        lstViewAdv.Items.Clear()
        For Each khib In Partager.refObj.lstRefAdv
            lstViewAdv.Items.Add(New ListViewItem(New String() {khib.id, khib.nom}))
        Next
    End Sub

    Private Sub Btn_ajou_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_ajou.Click

        Try
            Dim strCltError As String = "a"
            Dim strAdvError As String = "a"
            Dim idClt As Integer = 0
            Dim idAdv As Integer = 0
            Dim flag As Boolean

            If txt_Ref.Text <> "" Then


                If Comb_nom.SelectedIndex < 0 And Comb_adv.SelectedIndex < 0 Then
                    MsgBox("اختر موكل أو خصم أولا", MsgBoxStyle.Critical, "خطأ")
                Else

                    '///////////////////////////////// Partie 1 de l'idée 
                    'Voir si les parties ont déjà un Ref
                    If List_Clients.Items.Count > 0 And lstViewAdv.Items.Count > 0 Then ' si les deux combo sont plein
                        MsgBox("المرجع مرتبط بأطراف أخرى", MsgBoxStyle.Critical, "خطأ")
                        Exit Sub
                    ElseIf List_Clients.Items.Count > 0 Xor lstViewAdv.Items.Count > 0 Then 'si l'une deux est remplie sans l'autre
                        If List_Clients.Items.Count > 0 Then
                            List_Clients.Items(0).Selected = True
                            idClt = CInt(List_Clients.SelectedItems(0).SubItems(0).Text)
                            If Comb_adv.SelectedIndex > -1 Then
                                Partager.deClt = DirectCast(Comb_adv.SelectedItem, DictionaryEntry)
                                idAdv = Partager.deClt.Key
                            End If
                            flag = Partager.partiesOntRef(idAdv, idClt)
                        Else
                            lstViewAdv.Items(0).Selected = True
                            idAdv = CInt(lstViewAdv.SelectedItems(0).SubItems(0).Text)
                            If Comb_adv.SelectedIndex > -1 Then
                                Partager.deClt = DirectCast(Comb_nom.SelectedItem, DictionaryEntry)
                                idClt = Partager.deClt.Key
                            End If
                            flag = Partager.partiesOntRef(idAdv, idClt)
                        End If
                    Else 'si aucune n'est remplie
                        If Comb_adv.SelectedIndex > -1 Then
                            Partager.deClt = DirectCast(Comb_adv.SelectedItem, DictionaryEntry)
                            idAdv = Partager.deClt.Key
                        End If
                        If Comb_adv.SelectedIndex > -1 Then
                            Partager.deClt = DirectCast(Comb_nom.SelectedItem, DictionaryEntry)
                            idClt = Partager.deClt.Key
                        End If
                        flag = Partager.partiesOntRef(idAdv, idClt)
                    End If
                    '////////////////////////// Fin

                    If flag Then ' je suis là
                        MsgBox("هاته الأطراف لديها مرجع من قبل", MsgBoxStyle.Critical, "خطأ")
                    Else

                        If MsgBox("هل تريد فعلا إضافة هذه الأطراف للمرجع ؟", MsgBoxStyle.YesNo Or MsgBoxStyle.Information, "إضافة") = MsgBoxResult.Yes Then
                            Dim bol As Boolean = False

                            '//////////////////////
                            If Comb_nom.SelectedIndex > -1 Then
                                If List_Clients.Items.Count < 1 Then
                                    Partager.deClt = DirectCast(Comb_nom.SelectedItem, DictionaryEntry)
                                    strCltError = Partager.refObj.ajouterClient(Partager.deClt.Key)
                                    remplirListViewClt()
                                Else
                                    MsgBox("لايمكن إضافة أكثر من موكل واحد", MsgBoxStyle.Critical, "خطأ")
                                End If
                            End If
                            '////////////////
                            If Comb_adv.SelectedIndex > -1 Then
                                If lstViewAdv.Items.Count < 1 Then
                                    Partager.deClt = DirectCast(Comb_adv.SelectedItem, DictionaryEntry)
                                    strAdvError = Partager.refObj.ajouterAdv(Partager.deClt.Key)
                                    remplirListViewAdv()
                                Else
                                    MsgBox("لايمكن إضافة أكثر من خصم واحد", MsgBoxStyle.Critical, "خطأ")
                                End If
                            End If

                            Dim strMsg As String = ""
                            If strCltError = "" Xor strAdvError = "" Then
                                If strCltError = "" Then
                                    strMsg = "تمت إضافة الموكل بنجاح"
                                ElseIf strAdvError = "" Then
                                    strMsg = "تمت إضافة الخصم بنجاح"
                                End If
                            ElseIf strCltError = "" Xor strAdvError = "" Then
                                strMsg = "تمت إضافة الموكل و الخصم بنجاح"
                            End If

                            If strMsg <> "" Then
                                MsgBox(strMsg)
                            End If
                        End If

                    End If
                End If
            Else
                MsgBox("المرجو إضافة المرجع أولا", MsgBoxStyle.Critical, "المرجع")
            End If


            If strCltError.Contains("PK_refClt") Then
                MsgBox("تمت إضافة الموكل للمرجع من قبل", MsgBoxStyle.Critical, "خطأ")
            End If

            If strAdvError.Contains("PK_refAdv") Then
                MsgBox("تمت إضافة الخصم للمرجع من قبل", MsgBoxStyle.Critical, "خطأ")
            End If

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub Btn_supp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_supp.Click
        Try

            If List_Clients.SelectedIndices.Count < 1 And lstViewAdv.SelectedIndices.Count < 1 Then
                If lstViewAdv.SelectedIndices.Count < 1 Then
                    MsgBox("المرجو اختيار الخصم أولا", MsgBoxStyle.Critical, "خطأ")
                Else
                    MsgBox("المرجو اختيار الموكل أولا", MsgBoxStyle.Critical, "خطأ")
                End If
            End If

            If List_Clients.SelectedIndices.Count > 0 Then
                If MsgBox("هل تريد فعلا حذف هذا الموكل للمرجع ؟", MsgBoxStyle.YesNo Or MsgBoxStyle.Information, "حذف") = MsgBoxResult.Yes Then
                    Partager.refObj.suprimmer_Client(CInt(List_Clients.SelectedItems(0).SubItems(0).Text))
                    MsgBox("تمت عملية الحذف بنجاح", MsgBoxStyle.Information, "نجاح")
                    remplirListViewClt()
                End If
            End If

            If lstViewAdv.SelectedIndices.Count > 0 Then
                If MsgBox("هل تريد فعلا حذف هذا الخصم للمرجع ؟", MsgBoxStyle.YesNo Or MsgBoxStyle.Information, "حذف") = MsgBoxResult.Yes Then
                    Partager.refObj.suprimmer_Adversaire(CInt(lstViewAdv.SelectedItems(0).SubItems(0).Text))
                    MsgBox("تمت عملية الحذف بنجاح", MsgBoxStyle.Information, "نجاح")
                    remplirListViewAdv()
                End If
            End If

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub Btn_nouv_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_nouv.Click
        initialCombo()
    End Sub

    Private Sub Btn_parc_marji3ona_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_parc_mowakil.Click
        Me.Enabled = False
        Partager.frmMoule = New Form
        Partager.frmMoule = Me
        Partager.type_rech_mowakil = "Frm_Ref"
        Frm_Rech_Clients.Show()
    End Sub

    Private Sub Btn_parc_nom_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_parc_nom.Click
        Me.Enabled = False
        Partager.frmMoule = New Form
        Partager.frmMoule = Me
        Partager.type_rech_adver = "Frm_Idafa_khossoum"
        Frm_Rech_Adver.Show()
    End Sub

    Private Sub txt_Ref_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txt_Ref.TextChanged
        remplirListViewAdv()
        remplirListViewClt()
    End Sub

    Private Sub btnRef_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRef.Click
        Me.Enabled = False
        Frm_Ref1.Show()
    End Sub
End Class